package org.brisling.sysman.service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.sysman.domain.JobRolesPermission;
import org.brisling.sysman.repository.JobrolePermissionRepo;
import org.brisling.sysman.service.base.BaseSysmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;


@RestController
@RequestMapping("/jobrolePermission")
/**
 * 用戶后台服務service
 * @Author : jackson wang
 * @Date   : 2015年6月8日 下午4:18:01
 * @version: 1.0
 */
public class JobrolePermissionService extends BaseSysmanService<JobRolesPermission>{
	
		
	@Autowired
	private JobrolePermissionRepo baseRepo;	

	
	public JobrolePermissionService(){
		this.moduleName = ConstSysParam.MODULE_JOBROLE_PERMISSION;
		this.clazz = JobRolesPermission.class;
	}
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getListDomain.action")
	public ListDomain<JobRolesPermission> getJobPermissionListDomain(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){		
				
		return super.getPagedList(page, rows,getSort(sort));
			
	}
	
	/**
	 * 根据角色编码查询权限清单
	 * @param roleid 	
	 * @return	
	 */
	@RequestMapping("/getPermissionByJobcode.action")
	public List<JobRolesPermission> getPermissionByJobcode(@RequestParam(value="jobcode",required=false) Long jobcode		
			){		
		if((jobcode!=null)&&(jobcode>0))
			return baseRepo.findPermissionByJobcode(jobcode);
		return null;			
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delFlowRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		
			return delRow(id,ids);
		
	}
	
	
	
	
	/**
	 * 批量保存岗位角色权限配置
	 * @param id
	 */
	@RequestMapping("/save.action")
	public List<JobRolesPermission> save(
			@RequestBody(required=false) String body){
		
		String _body = null;
		try {
			_body = java.net.URLDecoder.decode(body,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);	
		JavaType _javaType = mapper.getTypeFactory().constructCollectionType(List.class, JobRolesPermission.class);
		List<JobRolesPermission> list = null;
		try {
			list =  mapper.readValue(_body, _javaType);
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
		
		if(list!=null){			
			if(list!=null && (list.size()>0)){				
				Long _jobcode = list.get(0).getJobcode();
				
				super.saveList(list);				
				
				//刷新权限
				if(_jobcode!=null){
					list = this.getPermissionByJobcode(_jobcode);
				}
			}	
			return list;
		}
		return null;
	}
	
}
